<template>
	<view class="container">
		<view class="login-header">
			<image class="logo" src="/static/images/logo.png" ></image>
			<text class="title">城市服务管理小程序</text>
		</view>
		
		<view class="login-form">
			<view class="form-item">
				<text class="label">手机号码</text>
				<input class="input" v-model="form.phone" placeholder="请输入手机号码" maxlength="11" type="number" />
			</view>
			
			<view class="form-item">
				<text class="label">密码</text>
				<input class="input" v-model="form.password" placeholder="请输入密码" type="password" />
			</view>
			
			<view class="login-btn" @click="handleLogin">登录</view>
			
			<view class="actions">
				<text class="action-link" @click="goToRegister">注册账号</text>
				<text class="action-link">忘记密码</text>
			</view>
		</view>
		
		<view class="wechat-login">
			<view class="divider">
				<text class="divider-text">或</text>
			</view>
			
			<button class="wechat-btn" open-type="getUserInfo" @getuserinfo="handleWechatLogin">
				<u-icon name="weixin-fill" color="#ffffff" size="30"></u-icon>
				<text>微信一键登录</text>
			</button>
		</view>
		
		<view class="agreement">
			<text class="agreement-text">登录即表示同意</text>
			<text class="agreement-link">《用户协议》</text>
			<text class="agreement-text">和</text>
			<text class="agreement-link">《隐私政策》</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					phone: '',
					password: ''
				}
			}
		},
		methods: {
			// 账号密码登录
			handleLogin() {
				// 表单验证
				if (!this.form.phone) {
					uni.showToast({
						title: '请输入手机号码',
						icon: 'none'
					});
					return;
				}
				
				if (!/^1\d{10}$/.test(this.form.phone)) {
					uni.showToast({
						title: '手机号码格式不正确',
						icon: 'none'
					});
					return;
				}
				
				if (!this.form.password) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return;
				}
				
				uni.showLoading({
					title: '登录中...'
				});
				
				// 调用登录接口
				this.$api.login(this.form)
					.then(res => {
						this.loginSuccess(res);
					})
					.catch(err => {
						console.error('登录失败:', err);
						uni.hideLoading();
						uni.showToast({
							title: err.message || '登录失败，请稍后重试',
							icon: 'none'
						});
					});
			},
			
			// 微信登录
			handleWechatLogin(e) {
				if (!e.detail.userInfo) {
					uni.showToast({
						title: '您已取消授权登录',
						icon: 'none'
					});
					return;
				}
				
				uni.showLoading({
					title: '登录中...'
				});
				
				// 获取微信登录凭证
				uni.login({
					provider: 'weixin',
					success: loginRes => {
						if (loginRes.code) {
							// 调用微信登录接口
							this.$api.wechatLogin({
								code: loginRes.code,
								userInfo: e.detail.userInfo
							})
							.then(res => {
								this.loginSuccess(res);
							})
							.catch(err => {
								console.error('微信登录失败:', err);
								uni.hideLoading();
								uni.showToast({
									title: err.message || '登录失败，请稍后重试',
									icon: 'none'
								});
							});
						} else {
							uni.hideLoading();
							uni.showToast({
								title: '微信登录授权失败',
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.hideLoading();
						uni.showToast({
							title: '微信登录授权失败',
							icon: 'none'
						});
					}
				});
			},
			
			// 登录成功处理
			loginSuccess(data) {
				// 隐藏加载
				uni.hideLoading();
				
				// 保存用户信息到本地存储
				uni.setStorageSync('userInfo', data);
				uni.setStorageSync('token', data.token);
				
				// 提示
				uni.showToast({
					title: '登录成功',
					icon: 'success'
				});
				
				// 跳转到首页
				setTimeout(() => {
					uni.switchTab({
						url: '/pages/index/index'
					});
				}, 1500);
			},
			
			// 跳转到注册页面
			goToRegister() {
				uni.navigateTo({
					url: '/pages/user/register'
				});
			}
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 40rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	
	.login-header {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 80rpx;
		padding-top: 60rpx;
	}
	
	.logo {
		width: 180rpx;
		height: 180rpx;
		margin-bottom: 30rpx;
	}
	
	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}
	
	.login-form {
		width: 100%;
		margin-bottom: 60rpx;
	}
	
	.form-item {
		margin-bottom: 40rpx;
	}
	
	.label {
		display: block;
		font-size: 28rpx;
		color: #666;
		margin-bottom: 20rpx;
	}
	
	.input {
		width: 100%;
		height: 90rpx;
		background-color: #f5f5f5;
		border-radius: 45rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		font-size: 28rpx;
	}
	
	.login-btn {
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		background-color: #007aff;
		color: #ffffff;
		text-align: center;
		font-size: 32rpx;
		border-radius: 45rpx;
		margin-top: 60rpx;
	}
	
	.actions {
		display: flex;
		justify-content: space-between;
		margin-top: 30rpx;
		padding: 0 20rpx;
	}
	
	.action-link {
		font-size: 26rpx;
		color: #007aff;
	}
	
	.wechat-login {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 60rpx;
	}
	
	.divider {
		position: relative;
		width: 100%;
		height: 1px;
		background-color: #e0e0e0;
		margin-bottom: 50rpx;
	}
	
	.divider-text {
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		background-color: #ffffff;
		padding: 0 30rpx;
		font-size: 26rpx;
		color: #999;
	}
	
	.wechat-btn {
		width: 100%;
		height: 90rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #07c160;
		color: #ffffff;
		border-radius: 45rpx;
		font-size: 32rpx;
	}
	
	.wechat-btn text {
		margin-left: 10rpx;
	}
	
	.agreement {
		position: fixed;
		bottom: 60rpx;
		display: flex;
		justify-content: center;
		font-size: 24rpx;
	}
	
	.agreement-text {
		color: #999;
	}
	
	.agreement-link {
		color: #007aff;
	}
</style>
